<!--
  * 云喇叭表单
-->
<template>
  <a-modal title="设备测试" :width="600" :open="visibleFlag" @cancel="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="formData" :rules="rules" layout="vertical">
      <a-form-item label="支付金额" name="message">
        <a-input v-model:value="formData.message" placeholder="请输入支付金额" />
      </a-form-item>
    </a-form>
    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">验证</a-button>
        <slot></slot>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick, computed, watch } from 'vue'
  import _ from 'lodash'
  import { message } from 'ant-design-vue'
  import { SmartLoading } from '/@/components/framework/smart-loading'
  import { smartSentry } from '/@/lib/smart-sentry'
  import { deviceApi } from '/@/api/business/b-operation/device'
  import { useRoute } from 'vue-router'
  const route = useRoute()
  // ---------------------------- 表格列 ----------------------------
  const cloudDevice = computed(() => {
    // 0云喇叭 1云打印
    return route.path.includes('cloud-speaker') ? 0 : 1
  })

  const props = defineProps({
    open: {
      type: Boolean,
      default: false,
    },
    // 表单类型 add/update/view
    // add: 新增, update: 编辑,view: 查看
    formType: {
      type: String,
      default: 'add',
    },
    rowData: {
      type: Object,
      default: () => {},
    },
  })
  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['ok', 'update:open'])
  const visibleFlag = computed({ get: () => props.open, set: (val) => emits('update:open', val) })

  // ------------------------ 显示与隐藏 ------------------------
  function onClose() {
    Object.assign(formData.value, formDefault)
    visibleFlag.value = false
    nextTick(() => {
      formRef.value?.clearValidate()
    })
  }

  // ------------------------ 获取表单数据 ------------------------
  async function getFormData(data) {
    SmartLoading.show()
    try {
      let res = await deviceApi.findId(data)
      formData.value = { ...res.data }
    } catch (err) {
      smartSentry.captureError(err)
    } finally {
      SmartLoading.hide()
    }
  }
  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref()

  const formDefault = {
    message: undefined,
  }

  let formData = ref({ ...formDefault })

  const rules = {
    message: [{ required: true, message: '支付金额 必填' }],
  }

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields()
      save()
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!')
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show()
    try {
      // let data = { ...formData.value, ...props.rowData,deviceSN: props.rowData.sn}
      let data = {
        deviceSN: props.rowData.sn,
        ticketInfo: JSON.stringify({
          detail: [
            ['品名', '单价', '数量', '金额'],
            ['酸梅汤', '2.5', '2', '5.0'],
            ['可乐', '5.0', '2', '10.0'],
            ['薯片', '5.0', '2', '10.0'],
            ['雪碧', '5.0', '2', '10.0'],
            ['冰红茶', '2.5', '2', '5.0'],
          ],
          ordertime: '2020-6-13 12:10:59',
          totalmoney: '44.0',
        }),
        message: formData.value.message,
      }
      await deviceApi.broadcast(data)
      message.success('操作成功')
      emits('ok')
      // onClose()
    } catch (err) {
      smartSentry.captureError(err)
    } finally {
      SmartLoading.hide()
    }
  }

  watch(
    [() => props.rowData, () => props.open],
    (val) => {
      if (props.open && props.rowData && props.formType != 'add') {
        getFormData(val[0])
      }
    },
    { immediate: true },
  )
</script>
